// packageFT/travel-order-client/feedback/feedback.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      rate: 0,
      content: '',
      img: []
    },
    oid: 0,
    api: ''
  },

  /**
   * 数据双向绑定
   * @param {Object} e 
   */
  updateFormData: function(e) {
    const { formdatakey } = e.currentTarget.dataset
    const value = e.detail
    const key = 'formData.' + [formdatakey]
    this.setData({
      [key]: value 
    })
  },

  /**
   * 图片选中上传(多图)
   * @param {Object} e 
   */
  afterRead: function(e) {
    const { file = [] } = e.detail
    let promiseArray = []
    const uploaderUrl = app.getNetAddresss(app.data.uploaderPicUrl)
    wx.showLoading({
      title: '上传中',
    })
    file.forEach(item => {
      promiseArray.push(new Promise((resolve, reject) => {
        wx.uploadFile({
          filePath: item.url,
          name: 'file',
          url: uploaderUrl,
          success: function(res) {
            resolve(res)
          },
          fail: function(err) {
            reject(err)
          }
        })
      }))
    })
    Promise.all(promiseArray).then(resArray => {
      wx.hideLoading()
      const addUrlArray = resArray.map(item => {
        const resData = JSON.parse(item.data)
        return {url: resData.data.img_url}
      })
      const { formData } = this.data
      const { field } = e.currentTarget.dataset
      const currentImgUrls = [...this.data.formData[field], ...addUrlArray]
      formData[field] = currentImgUrls
      this.setData({ formData })
    }).catch(err => {
      wx.hideLoading()
    })
  },

  /**
   * 删除已上传的图片
   * @param {Object} e 
   */
  deleteFile: function(e) {
    const { index } = e.detail
    const { field } = e.currentTarget.dataset
    let currentData = this.data.formData[field]
    const key = `formData.${field}`
    currentData.splice(index, 1)
    this.setData({
      [key]: currentData
    })
  },

  /**
   * 提交申请
   */
  submit: function() {
    const { formData, api, oid } = this.data
    const { rate, content, img } = formData
    if (!rate) {
      app.tips('请选择评分！')
      return
    }
    if (!oid) {
      app.tips('无订单信息！')
      return
    }
    if (!api) {
      app.tips('无接口地址！')
      return
    }
    const _this = this
    const urlStr = app.getNetAddresss(api)
    const images = img.map(item => {
      return item.url
    })
    const params = {
      oid,
      content,
      star: rate,
      images
    }
    wx.showLoading({ title: '正在提交...' })
    app._postNetWork({
      url: urlStr,
      data: params,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          app.tips(resData.msg || '评论成功')
          setTimeout(() => {
            wx.redirectTo({
              url: '/packageFT/travel-order-client/list/list?status=0&title=导游订单',
            })
          }, 3000)
        } else {
          app.tips(resData.msg || '操作失败！')
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      oid: options?.oid || 0,
      api: options.api || ''
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})